﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using pwblog.Entities;
using pwblog.BusinessObject;
using System.IO;

namespace pwblog.Web.Creator
{
    public class Published
    {
        public int Val { get; set; }
        public string Name { get; set; }
    }
    public partial class ArticlesCreator : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.FillArticlesGrid();
            }
        }

        /// <summary>
        /// Tải dữ liệu lên GridView
        /// </summary>
        protected void FillArticlesGrid()
        {
            Article[] articles = new ArticleBo().GetArticlesByUserName(User.Identity.Name);
            if (articles.Length > 0)
            {
                gvArticles.DataSource = articles;
                gvArticles.DataBind();
            }
        }

        /// <summary>
        /// Sự kiện tải dữ liệu vào một dòng trong GridView
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">GridViewRowEventArgs</param>
        protected void gvArticles_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (!String.IsNullOrEmpty(e.Row.Cells[3].Text))
                    e.Row.Cells[3].Text = DateTime.Parse(e.Row.Cells[3].Text).ToString(new CultureInfo("vi-VN", false).DateTimeFormat);
                DropDownList ddlUserName = (DropDownList)e.Row.FindControl("ddlUserName");
                if (ddlUserName != null)
                {
                    ddlUserName.DataSource = new AccountBo().GetUserNames();
                    ddlUserName.DataBind();
                    ddlUserName.SelectedValue = gvArticles.DataKeys[e.Row.RowIndex].Values[2].ToString();
                }

                DropDownList ddlCategory = (DropDownList)e.Row.FindControl("ddlCategory");
                if (ddlCategory != null)
                {
                    ddlCategory.DataSource = new CategoryBo().GetListItemCategories();
                    ddlCategory.DataBind();
                    ddlCategory.SelectedValue = gvArticles.DataKeys[e.Row.RowIndex].Values[1].ToString();
                }

                DropDownList ddlPublished = (DropDownList)e.Row.FindControl("ddlPublished");
                if (ddlPublished != null)
                {
                    ddlPublished.DataSource = new List<Published> { new Published { Val = 1, Name = "Đang xuất bản" }, new Published { Val = 0, Name = "Chưa xuất bản" } };
                    ddlPublished.DataBind();
                    ddlPublished.SelectedValue = gvArticles.DataKeys[e.Row.RowIndex].Values[4].ToString();
                }
                else
                {

                    if (gvArticles.DataKeys[e.Row.RowIndex].Values[4].ToString().Equals("1"))
                    {
                        e.Row.Cells[5].Text = "Đang xuất bản";
                    }
                    else
                    {
                        e.Row.Cells[5].Text = "Chưa xuất bản";
                    }
                }
            }
        }

        /// <summary>
        /// Sử lí sự kiện gvCategories_RowEditing khi click vào "Edit"
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">GridViewEditEventArgs</param>
        protected void gvArticles_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvArticles.EditIndex = e.NewEditIndex;
            FillArticlesGrid();
        }

        protected void gvArticles_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvArticles.EditIndex = -1;
            FillArticlesGrid();
        }

        /// <summary>
        /// Xử lí sự kiện gvCategories_RowUpdating khi click "Update"
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">GridViewUpdateEventArgs</param>
        protected void gvArticles_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int id = (int)gvArticles.DataKeys[e.RowIndex].Values[0];
            string nameArticle = ((TextBox)gvArticles.Rows[e.RowIndex].FindControl("txtNameArticle")).Text;
            string datePost = gvArticles.DataKeys[e.RowIndex].Values[3].ToString();
            string userName = User.Identity.Name;
            int idCategory = Int32.Parse(((DropDownList)gvArticles.Rows[e.RowIndex].FindControl("ddlCategory")).SelectedValue);
            int isPublished = Int32.Parse(((DropDownList)gvArticles.Rows[e.RowIndex].FindControl("ddlPublished")).SelectedValue);
            if (new ArticleBo().UpdateArticle(id, nameArticle, idCategory, datePost, userName, "", 0, isPublished, 0))
            {
                // thanh cong
            }
            else
            {
                // that bai
            }

            gvArticles.EditIndex = -1;
            FillArticlesGrid();
        }

        /// <summary>
        /// Xử lí sự kiện gvCategories_RowDeleting khi click vào "Delete"
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">GridViewDeleteEventArgs</param>
        protected void gvArticles_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = (int)gvArticles.DataKeys[e.RowIndex].Values[0];

            if (new ArticleBo().DeleteArticle(id))
            {
                // thanh cong
            }
            else
            {
                // that bai
            }

            FillArticlesGrid();
        }

        protected void gvArticles_lnkEditArticle_Click(object sender, CommandEventArgs e)
        {
            if (e.CommandArgument != null)
            {
                Response.Redirect(@"EditArticle.aspx?Id=" + e.CommandArgument.ToString());
            }
        }

        protected void gvArticles_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvArticles.PageIndex = e.NewPageIndex;
            FillArticlesGrid();
        }
    }
}